<template>
  <div class="login-container" :style="{background: 'url('+bg+')'}">
    <el-row>
      <el-col :span="5" :offset="2" class="top">
        <img class="logo animate__animated animate__backInLeft" :src="logo" alt />
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <div class="text">
          <p class="animate__animated animate__flipInY">好的书籍是最贵重的珍宝</p>
          <p class="line animate__animated animate__flipInY">别林斯基</p>
        </div>
      </el-col>
      <el-col :span="12">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form animate__animated animate__slideInDown"
          auto-complete="on"
          label-position="left"
        >
          <div class="title-container">
            <h3 class="title">欢迎登录</h3>
          </div>
          <el-form-item prop="username">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              v-model="loginForm.username"
              placeholder="Username"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>

          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="Password"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>

          <el-button
            :loading="loading"
            type="primary"
            style="width:100%;margin-bottom:30px;"
            @click.native.prevent="handleLogin"
          >Login</el-button>
          <div class="register">
            没有平台账号？
            <el-button v-preventReClick type="text" @click="openRegister">立即注册</el-button>
            <el-button v-preventReClick type="text" @click="dialogFormVisible2=true">忘记密码</el-button>
          </div>
        </el-form>
      </el-col>
    </el-row>
    <el-row class="copyright">
      <el-col>安徽萃智信息科技有限公司 版权所有</el-col>
    </el-row>
    <!-- 注册 -->
    <el-dialog
      class="register-dialog"
      title="注册用户"
      :close-on-click-modal="false"
      width="27%"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="formRegister" ref="registerForm" :rules="registerRules">
        <el-row>
          <div class="zy">
            请输入您的信息提交，需要管理员
            <span>审核</span> 通过方可使用
          </div>
        </el-row>
        <el-row>
          <el-col :span="10">
            <div>用户名：</div>
            <el-form-item>
              <el-input v-model="formRegister.username" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="4">
            <div>登陆密码：</div>
            <el-form-item>
              <el-input v-model="formRegister.password" autocomplete="off" show-password></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <div>确认密码：</div>
            <el-form-item>
              <el-input v-model="formRegister.repassword" autocomplete="off" show-password></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="4">
            <div>姓名：</div>
            <el-form-item>
              <el-input v-model="formRegister.realname" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <div>选择角色：</div>
            <el-form-item>
              <el-select v-model="formRegister.role_id" placeholder="请选择">
                <el-option :label="item.role_name" :value="item.role_id" v-for="item in roleList" :key="item.role_id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="4">
            <div>邮箱：</div>
            <el-form-item>
              <el-input v-model="formRegister.email" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <div>手机：</div>
            <el-form-item>
              <el-input v-model="formRegister.mobile" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="registerUser">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 找回密码 -->
    <el-dialog
      class="register-dialog"
      title="找回密码"
      :close-on-click-modal="false"
      width="27%"
      :visible.sync="dialogFormVisible2"
    >
      <el-form ref="form" :model="form" label-width="80px" class="form">
        <el-form-item label="邮箱">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="forgetPwd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";
import { forgetPassword,retrievePassword,selectRole,sign } from "@/api/user";

export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("Please enter the correct user name"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("The password can not be less than 6 digits"));
      } else {
        callback();
      }
    };
    return {
      logo: require("@/assets/login/logo.png"),
      bg: require("@/assets/login/bg.png"),

      loginForm: {
        username: "admin",
        password: "1234567",
      },
      loginRules: {
        // username: [
        //   { required: true, trigger: "blur", validator: validateUsername }
        // ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      formRegister: {
        username: "",
        password: "",
        repassword: "",
        realname: "", //姓名
        role_id: null,
        email: "",
        mobile: "",
      },
      registerRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      dialogFormVisible: false,
      dialogFormVisible2: false,
      loading: false,
      passwordType: "password",
      redirect: undefined,
      form: {
        email: "",
      },
      roleList:[]
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push("/page/library/cont");
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 忘记密码
    forgetPwd() {
      let params = {
        email: this.form.email,
      };
      forgetPassword(params).then((res) => {
        console.log(res);
      });
    },
    // 邮箱找回密码表单数据处理
    retrievePwd(){
      let params = {
        type: "", // int 是否通过验证 1通过
        password: "",// 修改的密码
        email: "",// 邮箱
        token: "",// 验证参数
      }
    },
    // 注册
    registerUser(){
      let params = this.formRegister;
      console.log(params)
      sign(params).then(res=>{
        console.log(res)
      })
    },
    // 用户注册获取角色分类
    getSelectRole(){
      selectRole().then(res=>{
        if( res.code === 1){
          this.roleList = res.data
        }
      })
    },
    openRegister(){
      this.dialogFormVisible = true;
      this.getSelectRole();
    }
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
/* reset element-ui css */
.login-container {
  background-size: 100%;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
.register-dialog {
  .el-input {
    height: 34px;
    input {
      height: 34px;
    }
  }
}
.el-dialog__body {
  border: 1px solid #ddd;
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;

  overflow: hidden;

  .login-form {
    // position: relative;
    width: 520px;
    max-width: 100%;
    padding: 120px 35px 20px;
    margin-left: 45px;
    // margin: 120px auto 0 auto;

    overflow: hidden;

    background-color: rgba($color: #000000, $alpha: 0.2);
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .line {
    position: relative;
    text-indent: 160px;
  }
  .line::after {
    position: absolute;
    left: 15.33333%;
    top: 40px;
    content: "";
    display: inline-block;
    width: 120px;
    height: 2px;
    background: rgba(255, 255, 255, 1);
  }
  .top {
    padding-top: 40px;
  }
  .logo {
    width: 100%;
  }
  .el-col-14 {
    height: 480px;
  }
  .text {
    height: 100%;
    width: 100%;
    padding: 80px 0 0 0;
    p {
      padding-left: 15.33333%;
      padding-top: 20px;
    }
    P:first-child {
      font-size: 64px;
      font-family: Microsoft YaHei;
      font-weight: 300;
      color: rgba(255, 255, 255, 1);
    }
    p:last-child {
      font-size: 36px;
      font-family: Microsoft YaHei;
      font-weight: 300;
      color: rgba(255, 255, 255, 1);
    }
  }
  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

.register {
  padding: 0 0 20px;
  font-size: 14px;
  line-height: 1.42857143;
  color: #fff;
  span {
    display: inline-block;
    padding-left: 20px;
    color: #409eff;
    font-size: 16px;
    cursor: pointer;
  }
}
.zy {
  background-image: -webkit-linear-gradient(top, #d9edf7 0, #b9def0 100%);
  background-image: -o-linear-gradient(top, #d9edf7 0, #b9def0 100%);
  background-image: -webkit-gradient(
    linear,
    left top,
    left bottom,
    from(#d9edf7),
    to(#b9def0)
  );
  background-image: linear-gradient(to bottom, #d9edf7 0, #b9def0 100%);
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffd9edf7', endColorstr='#ffb9def0', GradientType=0);
  background-repeat: repeat-x;
  border-color: #9acfea;
  color: #31708f;
  background-color: #d9edf7;
  padding: 15px;
  margin-bottom: 20px;
  border: 1px solid transparent;
  border-radius: 4px;
}
.login-container {
  position: relative;
}
.copyright {
  position: absolute;
  bottom: 30px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 24px;
  font-family: Microsoft YaHei;
  font-weight: 300;
  color: rgba(255, 255, 255, 1);
  // margin-top: 300px;
}
.form {
  .el-form-item {
    background: #fff;
  }
  .el-form-item__content,.el-input {
    border-radius: 2px;
    background: rgba(0, 0, 0, 0.1) !important;
  }
}
</style>
